<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 16:54
 */
namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Base\Power;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class SearchEmployeeController extends Controller
{
    public function searchEmployee(Power $power)
    {

        $data['employeeData'] = DB::table('employee')
            ->where('employee_state','=',1)
            ->where('employee_power','!=',$power->laozong()->power_id)
            ->leftJoin('area','area_id','=','employee_area')
            ->leftJoin('power','power_id','=','employee_power')
            ->get();
        return view("admin.searchEmployee",$data);
    }

    public function checkEmployee(BaseFunc $baseFunc)
    {
        $data['areaSelect'] = DB::table('area')->get();
        $areaData = Input::only('area_id');
        $employeeData = DB::table('employee')
            ->where('employee_state','=',0)
            ->leftJoin('area','area_id','=','employee_area')
            ->get();
        if($employeeData)
        {
            if($areaData['area_id'])
            {
                $data['checkEmployee'] = DB::table('employee')
                    ->where('employee_state','=',0)
                    ->where('employee_area','=',$areaData['area_id'])
                    ->leftJoin('area','area_id','=','employee_area')
                    ->get();
                return view("admin.checkEmployee",$data);
            }
            else
            {
                $data['checkEmployee'] = DB::table('employee')
                    ->where('employee_state','=',0)
                    ->leftJoin('area','area_id','=','employee_area')
                    ->get();
                return view("admin.checkEmployee",$data);
            }
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"目前还没有人要申请成为员工，暂时无法进入审核申请页面",NULL,"/searchEmployee");
        }
    }

    public function checkEmployees(BaseFunc $baseFunc)
    {
        $data = Input::only('success');
        $state['employee_state'] = 1;
        $state['employee_time'] = date("Y-m-d H:i:s");
        DB::table('employee')
            ->where('employee_id','=',$data['success'])
            ->update($state);
        $baseFunc->setRedirectMessage(true,"恭喜！您拥有了一位新员工！",NULL,"/searchEmployee");
    }

    public function delcheckEmployees(BaseFunc $baseFunc)
    {
        $data = Input::only('false');
        DB::table('employee')
            ->where('employee_id','=',$data['false'])
            ->delete();
        $baseFunc->setRedirectMessage(false,"此人申请未通过，已清除信息",NULL,"/searchEmployee");
    }
}